﻿using System;
using Hadlow.Jobvertizer.Models;
using Hadlow.Jobvertizer.Repositories;
using System.Collections.Specialized;
using Castle.MonoRail.Framework;

namespace Hadlow.Jobvertizer.Controllers
{
    public class PosterLoginController : BaseController
    {
        ISecurityHelper securityHelper;
        IRepository<User> userRepository;
        IRepository<Poster> posterRepository;
        IRepository<CardType> cardTypeRepository;
        IRepository<CreditCard> creditCardRepository;

        public PosterLoginController(
            ISecurityHelper securityHelper,
            IRepository<User> userRepository,
            IRepository<Poster> posterRepository,
            IRepository<CardType> cardTypeRepository,
            IRepository<CreditCard> creditCardRepository)
        {
            this.securityHelper = securityHelper;
            this.userRepository = userRepository;
            this.posterRepository = posterRepository;
            this.cardTypeRepository = cardTypeRepository;
            this.creditCardRepository = creditCardRepository;
        }

        public void Index()
        {
            // awkward hack here, we have to set the context for the security helper.
            securityHelper.Context = Context;

            if (securityHelper.UserIsLoggedIn)
            {
                if (securityHelper.CurrentUser.Role == Role.Poster)
                {
                    Redirect("Job", "JobForm");
                }
                RenderView("incorrectUserType");
                return;
            }

            CardType[] cardTypes = cardTypeRepository.GetAll();
            PropertyBag["cardTypes"] = cardTypes;
        }

        public void Login(string username, string password, bool rememberme, string returnUrl)
        {
            RedirectToLoginController(username, password, rememberme);
        }

        public void Register(
            string username, 
            string password, 
            string posterName, 
            [DataBind("creditCard")] CreditCard creditCard)
        {
            ValidateInput("user name", username);
            ValidateInput("password", password);
            ValidateInput("company name", posterName);

            User user = new User()
            {
                Name = username,
                Password = password,
                Role = Role.Poster
            };

            userRepository.Save(user);
            creditCardRepository.Save(creditCard);

            Poster poster = new Poster()
            {
                Name = posterName,
                ContactName = username,
                PosterUsers = new System.Collections.Generic.List<PosterUser>() { new PosterUser() { User = user } },
                CreditCard = creditCard
            };

            posterRepository.Save(poster);

            RedirectToLoginController(username, password, false);
        }

        private void RedirectToLoginController(string username, string password, bool rememberme)
        {
            NameValueCollection parameters = new NameValueCollection();
            parameters.Add("username", username);
            parameters.Add("password", password);
            parameters.Add("rememberme", rememberme.ToString());
            parameters.Add("returnUrl", "/Job/JobForm.castle");
            Redirect("Login", "Login", parameters);
        }

        private void ValidateInput(string friendlyName, string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                Flash["error"] = string.Format("You must supply a {0}. Try again.", friendlyName);
                RedirectToAction("Index");
            }
        }
    }
}
